<?php

namespace App\Http\Requests\Api;

use App\Models\User;
use Illuminate\Validation\Rule;

class LoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'type' => ['bail','required',Rule::in([User::LOGIN_TYPE_ACCOUNT,User::LOGIN_TYPE_PHONE])],
            'username' => ['bail','required_if:type,' . User::LOGIN_TYPE_ACCOUNT],
            'password' => 'bail|required_if:type,' . User::LOGIN_TYPE_ACCOUNT . '|alpha_dash|min:6',

            'phone' => 'bail|required_if:type,' . User::LOGIN_TYPE_PHONE . '|regex:/^1\d{10}$/',
            'phone_code' => 'bail|required_if:type,' . User::LOGIN_TYPE_PHONE . '|min:6|integer',
        ];
    }

    public function messages()
    {
        return [
            'type.required' => '请选择登录方式',
            'type.in' => '不被允许的登录方式',

            'username.required_if' => '请填写用户名',
            'password.required_if' => '请填写密码',
            'password.alpha_dash' => '密码不正确.',
            'password.min' => '密码不正确',

            'phone.required_if' => '请输入正确的手机号码',
            'phone.regex' => '请输入正确的手机号码',
            'phone_code.required_if' => '请输入验证码',
            'phone_code.min' => '验证码不正确.',
            'phone_code.integer' => '验证码不正确',

        ];
    }
}
